<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="标签编号" prop="labelCode">
        <el-input v-model="queryParams.labelCode" placeholder="请输入标签编号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="标签名称" prop="labelName">
        <el-input v-model="queryParams.labelName" placeholder="请输入标签名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="标签类型" prop="labelType">
        <el-select clearable v-model="queryParams.labelType" placeholder="请选择标签类型">
          <el-option v-for="(option, index) in typeList" :key="index" :value="option.dictValue"
            :label="option.dictLabel" />
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="字段排序" prop="sort">
        <el-input v-model="queryParams.sort" placeholder="请输入字段排序" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="是否删除：0未删除 1已删除 默认0" prop="isDel">
        <el-input v-model="queryParams.isDel" placeholder="请输入是否删除：0未删除 1已删除 默认0" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="创建人" prop="createUser">
        <el-input v-model="queryParams.createUser" placeholder="请输入创建人" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="修改人" prop="updateUser">
        <el-input v-model="queryParams.updateUser" placeholder="请输入修改人" clearable @keyup.enter.native="handleQuery" />
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['system:label:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['system:label:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['system:label:remove']">删除</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['system:label:export']">导出</el-button>
      </el-col> -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="labelList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" />
      <!-- <el-table-column label="标签类型" align="center" prop="labelType" /> -->
      <el-table-column label="标签编号" align="center" prop="labelCode" />
      <el-table-column label="标签名称" align="center" prop="labelName">
        <template slot-scope="scope">
          <typeTag :options="typeList" :value="scope.row.labelType" />
          <span>{{ scope.row.labelName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="添加时间" align="center" prop="createTime" />
      <el-table-column label="更新时间" align="center" prop="updateTime" />
      <!-- <el-table-column label="字段排序" align="center" prop="sort" /> -->
      <!-- <el-table-column label="备注[扩展使用]" align="center" prop="remark" /> -->
      <!-- <el-table-column label="是否删除：0未删除 1已删除 默认0" align="center" prop="isDel" /> -->
      <!-- <el-table-column label="标签状态：0：禁用  1：启用" align="center" prop="status"> -->
      <el-table-column label="标签状态" align="center" prop="status">
        <template slot-scope="scope">
          <!-- <div class="switch-box">
            <span class="def" @click="updateStatus(scope.row, 1)" :class="{ active: scope.row.status == 1 }">启动</span>
            <span class="def" @click="updateStatus(scope.row, 0)" :class="{ active: scope.row.status == 0 }">禁用</span>
          </div> -->
          <el-radio-group size="mini" v-model="scope.row.status" @input="e => { updateStatus(scope.row, e) }"
            v-if="isUpdateStatus">
            <el-radio-button :label="1">启用</el-radio-button>
            <el-radio-button :label="0">禁用</el-radio-button>
          </el-radio-group>
          <span v-else>
            <el-tag type="success" v-if="scope.row.status == 1">启动</el-tag>
            <el-tag type="danger" v-else-if="scope.row.status == 0">禁用</el-tag>
            <el-tag type="info" v-else>{{ scope.row.status }}</el-tag>
          </span>
          <!-- <el-switch v-model="scope.row.status" active-text="启用" inactive-text="禁用">
          </el-switch> -->
        </template>
      </el-table-column>
      <!-- <el-table-column label="创建人" align="center" prop="createUser" /> -->
      <!-- <el-table-column label="修改人" align="center" prop="updateUser" /> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['system:label:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['system:label:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改标签管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="标签编号" prop="labelCode">
          <el-input v-model="form.labelCode" placeholder="请输入标签编号" />
        </el-form-item>
        <el-form-item label="标签名称" prop="labelName">
          <el-input v-model="form.labelName" placeholder="请输入标签名称" />
        </el-form-item>
        <!-- <el-form-item label="字段排序" prop="sort">
          <el-input v-model="form.sort" placeholder="请输入字段排序" />
        </el-form-item> -->
        <!-- <el-form-item label="标签类型" prop="sort">
          <el-input v-model="form.sort" placeholder="请选择标签类型" />
        </el-form-item> -->
        <el-form-item label="标签类型" prop="labelType">
          <el-select clearable v-model="form.labelType" placeholder="请选择标签类型">
            <el-option v-for="(option, index) in typeList" :key="option.dictValue" :value="option.dictValue"
              :label="option.dictLabel" />
          </el-select>
        </el-form-item>
        <el-form-item label="标签状态" prop="status">
          <div class="switch-box">
            <span class="def" @click="choseAddStatus(1)" :class="{ active: form.status == 1 }">启动</span>
            <span class="def" @click="choseAddStatus(0)" :class="{ active: form.status == 0 }">禁用</span>
          </div>
          <!-- <el-switch v-model="form.status" active-text="启用" inactive-text="禁用">
          </el-switch> -->
        </el-form-item>
        <!-- <el-form-item label="备注[扩展使用]" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注[扩展使用]" />
        </el-form-item>
        <el-form-item label="是否删除：0未删除 1已删除 默认0" prop="isDel">
          <el-input v-model="form.isDel" placeholder="请输入是否删除：0未删除 1已删除 默认0" />
        </el-form-item>
        <el-form-item label="创建人" prop="createUser">
          <el-input v-model="form.createUser" placeholder="请输入创建人" />
        </el-form-item>
        <el-form-item label="修改人" prop="updateUser">
          <el-input v-model="form.updateUser" placeholder="请输入修改人" />
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getLabelType, updateStatus, listLabel, getLabel, delLabel, addLabel, updateLabel } from "@/api/system/label";
import typeTag from "./typeTag.vue";
export default {
  name: "Label",
  components: { typeTag },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 标签管理表格数据
      labelList: [],
      // 标签类型
      typeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        labelType: null,
        labelName: null,
        // labelCode: null,
        // sort: null,
        // isDel: null,
        // status: null,
        // createUser: null,
        // updateUser: null
      },
      // 表单参数
      form: {
        status: 1
      },
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getLabelType();
    this.getList();
  },
  computed: {
    isUpdateStatus() { return this.$auth.hasPermi('system:label:updateStatus') }
  },
  methods: {
    // 获取类型   // line_type：线路类型;  label_type:标签类型；  node_pool_type：节点池类型
    getLabelType() {
      this.loading = true;
      getLabelType({ dictType: 'label_type' }).then(response => {
        this.typeList = response.data || [];
        this.loading = false;
      });
    },
    /** 查询标签管理列表 */
    getList() {
      this.loading = true;
      listLabel(this.queryParams).then(response => {
        this.labelList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 标签禁用启用 */
    updateStatus(item, num) {
      console.log(item)
      let params = {
        id: item.id,
        status: num
      }
      updateStatus(params).then(response => {
        let msg = num == 1 ? '标签已启动' : '标签已禁用';
        this.$modal.msgSuccess(msg);
        this.getList()
      }).catch(err => {
        this.getList()
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        labelType: null,
        labelCode: null,
        labelName: null,
        sort: null,
        remark: null,
        isDel: null,
        status: 1,
        updateTime: null,
        createTime: null,
        createUser: null,
        updateUser: null
      };
      this.resetForm("form");
    },
    choseAddStatus(num) {
      this.form.status = num;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加标签管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getLabel(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改标签管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateLabel(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addLabel(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      let tagStr = '';
      this.typeList.forEach(item => {
        if (item.dictValue == row.labelType) {
          tagStr = item.dictLabel
        }
      });

      this.$modal.confirm('确认要删除标签管理' + tagStr + '[' + row.labelName + ']的数据项？').then(function () {
        return delLabel(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/label/export', {
        ...this.queryParams
      }, `label_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style>
.switch-box {
  width: 120px;
  border: 1px solid #EEEEEE;
  border-radius: 4px;
  overflow: hidden;
  height: 36px;
  display: flex;
  align-items: center;

  .def {
    width: 50%;
    text-align: center;
    line-height: 34px;
    height: 34px;
    vertical-align: middle;
    background-color: #FFFFFF;
    color: #FFFFFF;
    cursor: pointer;

    &.active {
      background-color: #1890ff;
      color: #FFFFFF;
    }
  }
}
</style>
